package ${basePackage}.${service}.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import lombok.RequiredArgsConstructor;
<#if haveBigDecimal>
import java.math.BigDecimal;
</#if>
<#if haveDate>
import java.util.Date;
</#if>
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import ${basePackage}.${entity}.${className}Entity;
import ${basePackage}.vo.${className}VO;
import ${basePackage}.pojo.Add${className}POJO;
import ${basePackage}.pojo.Update${className}POJO;
import ${basePackage}.${mapper}.${className}Mapper;
import ${basePackage}.${service}.I${className}Service;

/**
 * ${comment}111111
 *
 * @author ${author}
 * @date ${createTime}
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ${className}ServiceImpl extends ServiceImpl${r'<'}${className}Mapper, ${className}Entity> implements I${className}Service {

    private final ${className}Mapper ${className?uncap_first}Mapper;


    /**
     * 分页查询
     *
     * @param pageNum   分页参数
     * @param pageSize  分页参数
     * @param data      其他查询过滤参数
     * @return 分页列表数据
     */
    @Override
    public PageInfo${r'<'}${className}VO> pageList(int pageNum, int pageSize, ${className}VO data) {
        pageNum = Math.max(pageNum, 1);
        pageSize = pageSize < 1 || pageSize > 50 ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);

        List${r'<'}${className}VO> results = ${className?uncap_first}Mapper.select(data);

        return new PageInfo${r'<'}>(results == null ? Collections.emptyList() : results);
    }

    /**
     * 查询一条数据
     *
    <#list pkColumns as column>
     * @param ${column.attrName} ${column.columnComment}
    </#list>
     * @return 一条数据
     **/
    @Override
    public ${className}VO getOne(<#list pkColumns as column>${column.attrType} ${column.attrName}<#if column_has_next>, </#if></#list>) {
        ${className}VO result = ${className?uncap_first}Mapper.selectByPk(<#list pkColumns as column>${column.attrName}<#if column_has_next>, </#if></#list>);

        return result;
    }

    /**
     * 新增数据
     *
     * @param data         数据
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean add(Add${className}POJO data) {
        ${className}Entity param = new ${className}Entity();
        <#list columns as column>
            <#if (pkColumns?size == 1 && column.columnKey == "PRI")>
        param.set${column.upcaseAttrName}(GenerateCodeUtils.generateId());
            <#elseif (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser" || column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate" || column.attrName == "valid" || column.attrName == "deleted")>
            <#else>
        param.set${column.upcaseAttrName}(data.get${column.upcaseAttrName}());
            </#if>
        </#list>

        return this.save(param);
    }

    /**
     * 批量新增数据
     *
     * @param data        数据
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean addBatch(List${r'<'}Add${className}POJO> data) {
        List${r'<'}${className}Entity> params = new ArrayList<>();
        ${className}Entity param;
        for(Add${className}POJO item : data) {
            param = new ${className}Entity();
        <#list columns as column>
            <#if (pkColumns?size == 1 && column.columnKey == "PRI")>
            param.set${column.upcaseAttrName}(GenerateCodeUtils.generateId());
            <#elseif (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser" || column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate" || column.attrName == "valid" || column.attrName == "deleted")>
            <#else>
            param.set${column.upcaseAttrName}(item.get${column.upcaseAttrName}());
            </#if>
        </#list>
            params.add(param);
        }

        return this.saveBatch(params);
    }

    /**
     * 更新数据
     *
     * @param data         待更新数据
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean modify(Update${className}POJO data) {
        <#list pkColumns as column>
        if (data.get${column.upcaseAttrName}() == null) {
            return false;
        }
        </#list>
        ${className}Entity param = new ${className}Entity();
        <#list columns as column>
            <#if (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser" || column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate" || column.attrName == "valid" || column.attrName == "deleted")>
            <#else>
        param.set${column.upcaseAttrName}(data.get${column.upcaseAttrName}());
            </#if>
        </#list>

        return this.updateById(param);
    }

    /**
     * 移除一条数据
     *
    <#list pkColumns as column>
     * @param ${column.attrName} ${column.columnComment}
    </#list>
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean removeOne(<#list pkColumns as column>${column.attrType} ${column.attrName}<#if column_has_next>, </#if></#list>) {
        <#if (pkColumns?size == 1)>
        int result = ${className?uncap_first}Mapper.deleteById(${pkColumns[0].attrName});
        <#else>
        int result = ${className?uncap_first}Mapper.delete(new LambdaQueryWrapper${r'<'}${className}Entity>()<#list pkColumns as column>.eq(${className}Entity::get${column.attrName?cap_first}, ${column.attrName})</#list>);
        </#if>

        return result > 0;
    }
}
